<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            padding: 100px;
        }
        
        textarea {
            width: 200px;
            height: 100px;
            border: 1px solid pink;
            outline: none;
            resize: none;
        }
        
        ul {
            margin-top: 50px;
        }
        
        li {
            width: 300px;
            padding: 5px;
            background-color: rgb(245, 209, 243);
            color: red;
            font-size: 14px;
            margin: 15px 0;
        }
        
        li a {
            float: right;
        }
    </style>
</head>

<body>
    <textarea name="" id=""></textarea>
    <button id="btn1">发布</button>
    <ul>

    </ul>
    <button id="btn2">全部删除</button>
    <script>
        var btn = document.getElementById("btn1");
        var text = btn.previousElementSibling;
        var ul = btn.nextElementSibling;
        var btn2 = document.getElementById("btn2");
        if (text.value == "") {
            btn2.disabled == true;
        }
        btn.onclick = function() {
            if (text.value == "") {
                alert("您没有输入内容");
            } else {
                //创建li
                var li = document.createElement("li");
                //插入
                li.innerHTML = text.value + '<a href="javascript:;">删除</a>';
                ul.insertBefore(li, ul.children[0]); //在第一个位置插入
                alert("发布成功");
                //删除元素
                //寻找所有的a
                var as = document.querySelectorAll("a");
                for (var i = 0; i < as.length; i++) {
                    as[i].onclick = function() {
                        //删除的是a的父亲li
                        ul.removeChild(this.parentNode);
                    }
                }
            }
            text.value = "";
        }

        btn2.onclick = function() {
            if (text.value != null) {
                var as = document.querySelectorAll("a");
                for (var i = 0; i < as.length; i++) {
                    ul.removeChild(as[i].parentNode);
                }
            }
            alert("删除成功");
        }
    </script>
</body>

</html>